#include <iostream>

#include "doubleList.h"

using namespace std;

// array ending with a null 
void printDoubleArray(double array[],int size){
	cout<<"Current List is : ";
	if (size == 0) cout<<"Empty"<<endl;
	else {
		for(int i = 0 ; i < size; i++){
			cout<<"|"<<array[i];
		}
		cout<<"|"<<endl;
	}
	
}
int main(){

	doubleList	d1;
	double		*tempArray;
	int			size = 0 ;
	cout<<"======================================"<<endl;
	cout<<"Testing doubleList library"<<endl;
	cout<<"======================================"<<endl<<endl;
	cout<<"1.Testing insertTo and vectortoarray functions"<<endl;
	cout<<"1.";
	tempArray = d1.vectortoarray();
	printDoubleArray(tempArray,size);
	delete tempArray;

	if (!d1.insertTo(1,0.33)) cout<<"Cannot insert element 0.33 in position 1 (OK)"<<endl;
	else {
		cout<<"Inserted element 0.33 in position 1 (FAIL)"<<endl;
		size++;
	}

	cout<<endl<<"2.";
	tempArray = d1.vectortoarray();
	printDoubleArray(tempArray,size);
	delete tempArray;

	if (!d1.insertTo(0,0.33)) cout<<"Cannot insert element 0.33 in position 0 (FAIL)"<<endl;
	else {
		cout<<"Inserted element 0.33 in position 0 (OK)"<<endl;
		size++;
	}

	cout<<endl<<"3.";
	tempArray = d1.vectortoarray();
	printDoubleArray(tempArray,size);
	delete tempArray;

	if (!d1.insertTo(1,0))    cout<<"Cannot insert element 0 in position 1 (FAIL)"<<endl;
	else {
		cout<<"Inserted element 0 in position 1 (OK)"<<endl;
		size++;
	}

	cout<<endl<<"4.";
	tempArray = d1.vectortoarray();
	printDoubleArray(tempArray,size);
	delete tempArray;

	if (!d1.insertTo(0,-3.2)) cout<<"Cannot insert element -3.2 in position 0 (FAIL)"<<endl;
	else {
		cout<<"Inserted element -3.2 in position 0 (OK)"<<endl;
		size++;
	}
	
	cout<<endl<<"5.";
	tempArray = d1.vectortoarray();
	printDoubleArray(tempArray,size);
	delete tempArray;

	if (!d1.insertTo(0,0.33)) cout<<"Cannot insert element 0.33 in position 1 (FAIL)"<<endl;
	else {
		cout<<"Inserted element 0.33 in position 1 (OK)"<<endl;
		size++;
	}

	cout<<endl<<"6.";
	tempArray = d1.vectortoarray();
	printDoubleArray(tempArray,size);
	delete tempArray;

	if (!d1.insertTo(10,0.33)) cout<<"Cannot insert element 0.33 in position 10 (OK)"<<endl;
	else {
		cout<<"Inserted element 0.33 in position 10 (FAIL)"<<endl;
		size++;
	}

	cout<<endl<<"7.";
	tempArray = d1.vectortoarray();
	printDoubleArray(tempArray,size);
	delete tempArray;

	if (!d1.insertTo(3,2.987)) cout<<"Cannot insert element 2.987 in position 3 (FAIL)"<<endl;
	else {
		cout<<"Inserted element 2.987 in position 3 (OK)"<<endl;
		size++;
	}

	cout<<endl<<"8.";
	tempArray = d1.vectortoarray();
	printDoubleArray(tempArray,size);
	delete tempArray;

	if (!d1.insertTo(5,33.1)) cout<<"Cannot insert element 33.1 in position 5 (FAIL)"<<endl;
	else {
		cout<<"Inserted element 33.1 in position 5 (OK)"<<endl;
		size++;
	}

	cout<<endl<<"List After inserts should be 0.33|-3.2|0.33|2.987|0|33.1"<<endl;
	cout<<"and  ";
	tempArray = d1.vectortoarray();
	printDoubleArray(tempArray,size);
	delete tempArray;
	cout<<endl;

	cout<<endl<<"Testing of Functions insertTo,vectortoarray is successful"<<endl;
	

	cout<<"======================================"<<endl<<endl;
	cout<<"2.Testing remove_ifNumInPos and eraseNum functions"<<endl<<endl;


	
	cout<<endl<<"9.";
	tempArray = d1.vectortoarray();
	printDoubleArray(tempArray,size);
	delete tempArray;

	//|0.33|-3.2|0.33|2.987|0|33.1
	if (d1.remove_ifNumInPos(0,0.33))	{
		cout<<"Num 0.33 found in position 0 and deleted (OK)"<<endl;
		size--;
	}
	else cout<<"Num 0.33 not found in position 0"<<endl;

	cout<<endl<<"10.";
	tempArray = d1.vectortoarray();
	printDoubleArray(tempArray,size);
	delete tempArray;

	if (d1.remove_ifNumInPos(0,0.33))	{
		cout<<"Num 0.33 found in position 0 and deleted (FAIL)"<<endl;
		size--;
	}
	else cout<<"Num 0.33 not found in position 0(OK)"<<endl;

	cout<<endl<<"11.";
	tempArray = d1.vectortoarray();
	printDoubleArray(tempArray,size);
	delete tempArray;

	if (d1.remove_ifNumInPos(4,33.1)) {
		cout<<"Num 33.1 found in position 4 and deleted (OK)"<<endl;
		size--;
	}
	else cout<<"Num 33.1 not found in position 4 (FAIL)"<<endl;

	cout<<endl<<"12.";
	tempArray = d1.vectortoarray();
	printDoubleArray(tempArray,size);
	delete tempArray;

	if (d1.remove_ifNumInPos(-2,0.33))	 {
		cout<<"Num 0.33 found in position -2 and deleted (FAIL)"<<endl;
		size--;
	}
	else cout<<"Num 0.33 not found in position -2 (negative)(OK)"<<endl;

	cout<<endl<<"13.";
	tempArray = d1.vectortoarray();
	printDoubleArray(tempArray,size);
	delete tempArray;

	if (d1.remove_ifNumInPos(12,0.33))	 {
		cout<<"Num 0.33 found in position 12 and deleted (FAIL)"<<endl;
		size--;
	}
	else cout<<"Num 0.33 not found in position 12 (out of bounds)(OK)"<<endl;

	cout<<endl<<"List After remove_ifNumInPos should be |-3.2|0.33|2.987|0|"<<endl;
	cout<<"and  ";
	tempArray = d1.vectortoarray();
	printDoubleArray(tempArray,size);
	delete tempArray;
	cout<<endl;

	// testing erasenum

	double *cu;
	
	cout<<endl<<"14.";
	tempArray = d1.vectortoarray();
	printDoubleArray(tempArray,size);
	delete tempArray;

	cu=d1.erasenum();
	size--;
	cout<<"Number: "<<cu[0]<<" delete from  Position: "<<cu[1]<<endl;
	cout<<"Updated Vector:"<<endl;
	tempArray = d1.vectortoarray();
	printDoubleArray(tempArray,size);
	delete tempArray;

	cout<<endl<<"15.";

	cu=d1.erasenum();
	size--;
	cout<<"Number: "<<cu[0]<<" delete from  Position: "<<cu[1]<<endl;
	cout<<"Updated Vector:"<<endl;
	tempArray = d1.vectortoarray();
	printDoubleArray(tempArray,size);
	delete tempArray;

	cout<<endl<<"Testing of Functions remove_ifNumInPos and erasenum is successful"<<endl;

	cout<<"======================================"<<endl<<endl;
	cout<<"2.Testing reverseList substractlist functions"<<endl<<endl;
    
	cout<<endl<<"16.";
	tempArray = d1.vectortoarray();
	printDoubleArray(tempArray,size);
	delete tempArray;
	cout<<"Reverse:";
	d1.reverseList();
	tempArray = d1.vectortoarray();
	printDoubleArray(tempArray,size);
	delete tempArray;


	cout<<"Sub '1' from all elements after position 0(from start)"<<endl;
	d1.substractList(0,1);
	cout<<endl<<"17.";
	tempArray = d1.vectortoarray();
	printDoubleArray(tempArray,size);
	delete tempArray;
	
	cout<<"Sub '1' from all elements after position 1(from 2nd element)"<<endl;
	d1.substractList(1,1);
	cout<<endl<<"18.";
	tempArray = d1.vectortoarray();
	printDoubleArray(tempArray,size);
	delete tempArray;
	
	cout<<"Sub '1' from all elements after position 2(from third element) should print error"<<endl;
	d1.substractList(2,1);
	cout<<endl<<"19.";
	tempArray = d1.vectortoarray();
	printDoubleArray(tempArray,size);
	delete tempArray;

	cout<<"Sub 1st element from all elements after position 0 ( from 1st element) should delete new 0 element"<<endl;
    tempArray = d1.vectortoarray();
	int ret;
	if((ret = d1.substractList(0,tempArray[0])) != -1) size -= ret;
	delete tempArray;

	cout<<endl<<"20.";
	tempArray = d1.vectortoarray();
	printDoubleArray(tempArray,size);
	delete tempArray;

	// some inserts to populate
	d1.insertTo(0,3.3);
	d1.insertTo(0,2);
	d1.insertTo(0,-1);
	d1.insertTo(1,2);
	d1.insertTo(3,3.3);
	d1.insertTo(3,2.6);
	d1.insertTo(4,2.1);
	size += 7;
	/////////////////////

	cout<<endl;
	tempArray = d1.vectortoarray();
	printDoubleArray(tempArray,size);
	delete tempArray;

	cout<<"Sub 3.3 element from all elements after position 2 ( from 3rd element) should delete 2 elements "<<endl;

	if((ret = d1.substractList(2,3.3)) != -1) 
		size -= ret;


	cout<<endl<<"21.";
	tempArray = d1.vectortoarray();
	printDoubleArray(tempArray,size);
	delete tempArray;
	
	cout<<endl<<"22.";
	cout<<"Reverse:";
	d1.reverseList();
	tempArray = d1.vectortoarray();
	printDoubleArray(tempArray,size);
	delete tempArray;

	cout<<endl<<"Testing of Functions reverseLit substractlist is successful"<<endl;

	cout<<"======================================"<<endl<<endl;
	cout<<"3.Testing insert_rand,add_after functions"<<endl<<endl;

	int rand1, rand2,rand3;
    
	cout<<endl<<"23.";
    rand1 = d1.insert_rand(2.3);
    cout<<"1st execution of insert_rand(2.3) and random position is : " << rand1 <<endl;
	size++;
	tempArray = d1.vectortoarray();
	printDoubleArray(tempArray,size);
	delete tempArray;
      
	cout<<endl<<"24.";
    rand2 = d1.insert_rand(7.8);
    cout<<"2nd execution of insert_rand(7.8) and random position is : " << rand2 <<endl;
	size++;
	tempArray = d1.vectortoarray();
	printDoubleArray(tempArray,size);
	delete tempArray;
      
	cout<<endl<<"25.";
    rand3 = d1.insert_rand(10);
    cout<<"3rd execution of insert_rand(10) and random position is : " << rand3 <<endl;
	size++;
	tempArray = d1.vectortoarray();
	printDoubleArray(tempArray,size);
	delete tempArray;
      

	cout<<endl<<"26.";
	d1.add_after(0,2.2);
	cout<<"Adding 2.2 to all elements"<<endl;
	tempArray = d1.vectortoarray();
	printDoubleArray(tempArray,size);
	delete tempArray;

	cout<<endl<<"27.";
	d1.add_after(3,2.2);
	cout<<"Adding 2.2 to all elements after position 3 from (4th)"<<endl;
	tempArray = d1.vectortoarray();
	printDoubleArray(tempArray,size);
	delete tempArray;

	cout<<endl<<"28.";
	d1.add_after(13,2.2);
	cout<<"Adding 2.2 to all elements after position 13 from (14th) should print error"<<endl;
	tempArray = d1.vectortoarray();
	printDoubleArray(tempArray,size);
	delete tempArray;

	cout<<"All tests are passed , library is working"<<endl;
	system("Pause");

}